/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;


import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

/**
 * This page allows to get the following information in order to create the new plan
 *  - Agent file
 *  - Class generation parameters
 *  
 *  @author Pablo Muñoz
 */

public class AddPlanWizardPage1 extends WizardPage {
	private Text packageNameText;

	private Text agentFileText;
	
	private Button enableClassGeneration;
	
	private Text classNameText;
	
	private Text fileNameText;	

	private ISelection selection;

	/**
	 * Constructor for AddPlanWizardPage1
	 * 
	 * @param pageName
	 */
	public AddPlanWizardPage1(ISelection selection) {
		super("AddPlanWizardPage1");
		setTitle("Add Plan Wizard");
		setDescription("Page 1: ADF file selection");
		this.selection = selection;
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;
		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, "es.upm.dit.gsi.eclipse.jadex.adfmanager.addPlanWizard");
		
		Label label1= new Label(container, SWT.NULL);
		label1.setText("&Agent file:");

		agentFileText = new Text(container, SWT.BORDER | SWT.SINGLE);
		agentFileText.setEditable(false);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		agentFileText.setLayoutData(gd);
		agentFileText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		separator.setLayoutData(gd);
		gd.horizontalSpan = 2;		
		
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("Enable class generation");
		label3.setLayoutData(gd);
		enableClassGeneration = new Button (container, SWT.CHECK);
		enableClassGeneration.setSelection(false);
		enableClassGeneration.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		
		
		Label label = new Label(container, SWT.NULL);
		label.setText("&Package name:");
		packageNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		packageNameText.setLayoutData(gd);
		packageNameText.setEnabled(false);
		packageNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("&Class name:");
		classNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		classNameText.setLayoutData(gd);
		classNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				writeClassFileName();
				dialogChanged();				
			}
		});
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText("&File name:");
		fileNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		fileNameText.setEditable(false);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		fileNameText.setLayoutData(gd);
		fileNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		initialize();
		dialogChanged();
		enabledPropertiesChanged();
		setControl(container);
	}

	
	/*
	 * This method is called in order to initialize the controls status
	 */
	private void initialize() {
		if (selection != null && selection.isEmpty() == false
				&& selection instanceof IStructuredSelection) {
			IStructuredSelection ssel = (IStructuredSelection) selection;
			if (ssel.size() > 1)
				return;
			Object obj = ssel.getFirstElement();
			if (obj instanceof IFile) {
				IFile file = (IFile) obj;
				if(!file.getName().endsWith(".agent.xml")){
					return;
				}
				agentFileText.setText(file.getName());
				packageNameText.setText("");
				}			
			}
	}
	

	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void enabledPropertiesChanged(){
		packageNameText.setEnabled(enableClassGeneration.getSelection());
		fileNameText.setEnabled(enableClassGeneration.getSelection());
		classNameText.setEnabled(enableClassGeneration.getSelection());
		dialogChanged();
	}

	/**
	 * Ensures that all text fields are set.
	 */
	private void dialogChanged() {		
		if (agentFileText.getText().length() == 0) {
			updateStatus("Agent File must be specified");
			return;
		}

		if (packageNameText.getText().length() == 0 && enableClassGeneration.getSelection()) {
			updateStatus("Package name must be specified");
			return;
		}
		if((!JavaCore.isJavaLikeFileName(fileNameText.getText()))&& enableClassGeneration.getSelection()){
			updateStatus("Classname is not Java Like");
			return;
		}
		updateStatus(null);
	}

	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	/*
	 * This method is called in order to modify automatically the class file name 
	 * text field when the class name field is modified
	 */
	private void writeClassFileName(){
		fileNameText.setText(classNameText.getText() + ".java");		
	}

	/**
	 * 
	 * @return the agent file
	 */
	public IFile getAgentFile() {
		try{
			return (IFile)((IStructuredSelection)selection).getFirstElement();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	 * @return the agent source folder
	 */
	public IFolder getSourceFolder(){
		try{
			return getAgentFile().getProject().getFolder("src/main/java/");
		}
		catch(Exception e1){
			e1.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @return the package name
	 */
	public String getPackageName() {
		return packageNameText.getText();
	}
	
	/**
	 * 
	 * @return the class name for the new plan
	 */
	public String getPlanClassName(){
		if(classNameText.isEnabled()){
			return classNameText.getText();
		}
		else{
			return null;
		}
	}
	
	/**
	 * 
	 * @return the class file name for the new plan
	 */
	public String getPlanFileName(){
		if(fileNameText.isEnabled()){
			return fileNameText.getText();
		}
		else{
			return null;
		}
	}
}